﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace GameAutomationLibrary
{

    /// <summary>
    /// 
    /// </summary>
    public class GameRoleEntity
    {
        private WebBrowser _webBrowser1 = new WebBrowser();
        private CookieContainer _cookieHome = new CookieContainer();
        private GameRoleSetting _roleSettings = null;

        public GameRoleEntity(GameRoleSetting roleSettings)
        {
            _webBrowser1.Dock = DockStyle.Fill;
            _webBrowser1.Navigated += WebBrowser1Navigated;
            //_webBrowser1.Navigate(RoleSettings.LoginUrl);


        }

        public GameRoleSetting RoleSettings
        {
            get { return _roleSettings; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WebBrowser1Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {

            //process Login
            _webBrowser1.Navigated -= WebBrowser1Navigated;

            HtmlDocument doc = _webBrowser1.Document;
            HtmlElement htmlUid = doc.GetElementById("email");//用户名文本框ID
            htmlUid.SetAttribute("value", RoleSettings.LoginId);//你要填充的用户名
            HtmlElement htmlPwd = doc.GetElementById("password");//密码文本框ID
            htmlPwd.SetAttribute("value", RoleSettings.LoginPassword);//你要填充的密码文本
            HtmlElement htmlSubmitButton = doc.GetElementById("login");//提交按钮ID

            _webBrowser1.Navigated += WebBrowserLogin;
            htmlSubmitButton.InvokeMember("Click");


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WebBrowserLogin(object sender, WebBrowserNavigatedEventArgs e)
        {
            _webBrowser1.Navigated -= WebBrowserLogin;

            string cookieStr = _webBrowser1.Document.Cookie;
            string[] cookstr = cookieStr.Split(';');
            foreach (string str in cookstr)
            {
                string[] cookieNameValue = str.Split('=');
                Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
                ck.Domain = "www.google.com";
                _cookieHome.Add(ck);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Login()
        {
            string para = string.Format("?uid={0}&pwd={1}&r={2}",_roleSettings.LoginId,_roleSettings.LoginPassword,DateTime.Now .Ticks);
            string response = PostRequest(_roleSettings.LoginUrl, "Get", para);
        }

        /// <summary>
        /// 
        /// </summary>
        public string PostRequest(string uri,string postType,string paras)
        {
            
            //准备提交请求
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
            webRequest.Method = "Get";
            //webRequest.ContentType = "application/x-www-form-urlencoded";
            //附加前面保持的cookie
            webRequest.CookieContainer = _cookieHome;
            //提交请求获得响应
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            _cookieHome.Add(webResponse.Cookies);
            //抽取响应内容
            StringBuilder strBuff = new StringBuilder();
            using (Stream respStream = webResponse.GetResponseStream())
            {
                using (StreamReader respStreamReader = new StreamReader(respStream, Encoding.UTF8))
                {
                    strBuff.Append(respStreamReader.ReadToEnd());
                }
            }
            //todo:处理结果
            return strBuff.ToString();
        }
    }
}
